package com.four.bmm.controller;

import com.four.bmm.entity.Block;
import com.four.bmm.entity.BlockItem;
import com.four.bmm.entity.News;
import com.four.bmm.service.AdminService;
import com.four.bmm.service.BlockService;
import com.four.bmm.service.NewsService;
import com.four.bmm.util.JsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;


@WebServlet("/AdminNewsServlet")
public class AdminNewsServlet  extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       HttpSession session=request.getSession();
        //管理员可以查看所有新闻
        AdminService adminService=new AdminService();
        List<News> adminNewsList= adminService.adminGetNewsNoPage();
        request.setAttribute("adminNewsList",adminNewsList);

        //管理员可以查看所有一级版块
        BlockService blockService=new BlockService();
        List<Block>blockList=blockService.findAllBlock();
        request.setAttribute("blockList",blockList);
        //session.setAttribute("blockList",blockList);
        NewsService newsService=new NewsService();
        String keyword = request.getParameter("keyword");
        int count=newsService.NewsCount(keyword);
        request.setAttribute("count",count);
        //管理员可以查看所有二级版块
        List<BlockItem> blockItems=blockService.findAllBItem();
        request.setAttribute("blockItems",blockItems);
        session.setAttribute("blockItems",blockItems);

        request.getRequestDispatcher("back/article-list.jsp").forward(request,response);
        //JsonUtil.strToJson(response,adminNewsList);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
